package Demo;

import java.util.concurrent.BlockingDeque;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.LinkedBlockingQueue;

public class Demo16 {
    public static void main(String[] args) {
        BlockingQueue<Integer> blockingQueue = new LinkedBlockingQueue<>();
        // 生产者
        Thread t1 = new Thread(() ->{
            for (int i = 0; i < 1000; i++) {
                try {
                    blockingQueue.put(i);
                    System.out.println("生产元素: "+ i);
                    Thread.sleep(200);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        });
        t1.start();

        // 消费者
        Thread t2 = new Thread(() ->{
            while(true){
                try {
                    Integer ret = blockingQueue.take();
                    System.out.println("消费元素: "+ ret);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        });
        t2.start();
    }
}
